package com.atguigu.tingshu.album.api;

import com.atguigu.tingshu.album.service.TrackInfoService;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.common.util.AuthContextHolder;
import com.atguigu.tingshu.model.album.TrackInfo;
import com.atguigu.tingshu.query.album.TrackInfoQuery;
import com.atguigu.tingshu.vo.album.TrackInfoVo;
import com.atguigu.tingshu.vo.album.TrackListVo;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Map;

@Tag(name = "声音管理")
@RestController
@RequestMapping("api/album/trackInfo")
@SuppressWarnings({"all"})
public class TrackInfoApiController {

    @Autowired
    private TrackInfoService trackInfoService;


    @Operation(summary = "修改声音信息")
    @PutMapping ("/updateTrackInfo/{trackId}")
    public Result updateTrackInfo(@PathVariable Long trackId,@RequestBody TrackInfoVo trackInfoVo){
        //调用service方法
        trackInfoService.updateTrackInfo(trackInfoVo,trackId);
        return Result.ok();
    }

    /**
     * 根据声音id回显数据
      * @param trackId
     * @return
     */
    @Operation(summary = "根据声音id查询声音数据")
    @GetMapping("/getTrackInfo/{trackId}")
    public Result getTrackInfo(@PathVariable Long trackId) {
        //调用service方法
        TrackInfo trackInfo = trackInfoService.getTrackInfo(trackId);
        //返回数据结果
        return Result.ok(trackInfo);
    }

    /**
     * 根据id删除声音
     * @param trackId
     * @return
     */
    @Operation(summary = "根据声音id删除声音")
    @DeleteMapping("/removeTrackInfo/{trackId}")
    public Result removeTrackInfo(@PathVariable Long trackId) {
        trackInfoService.removeTrackInfoById(trackId);
        return Result.ok();
    }

    /**
     * 分页查询声音列表
     *
     * @param pageNo
     * @param pageSize
     * @param trackInfoQuery
     * @return
     */
    @Operation(summary = "查看声音列表")
    @PostMapping("/findUserTrackPage/{pageNo}/{pageSize}")
    public Result findUserTrackPage(@PathVariable Long pageNo,
                                    @PathVariable Long pageSize,
                                    @RequestBody TrackInfoQuery trackInfoQuery) {
        //获取用户id
        Long userId = AuthContextHolder.getUserId() == null ? 1L : AuthContextHolder.getUserId();
        trackInfoQuery.setUserId(userId);
        //构建分页对象
        Page<TrackListVo> page = new Page<>(pageNo, pageSize);
        //调用service方法
        IPage<TrackListVo> iPage = this.trackInfoService.getUserTrackPage(page, trackInfoQuery);
        return Result.ok(iPage);
    }

    /**
     * 保存声音
     * @param trackInfoVo
     * @return
     */

    @Operation(summary = "保存声音")
    @PostMapping("/saveTrackInfo")
    public Result saveTrackInfo(@RequestBody TrackInfoVo trackInfoVo) {
        //获取用户id
        Long userId = AuthContextHolder.getUserId() == null ? 1L : AuthContextHolder.getUserId();
        //调用service方法
        trackInfoService.saveTrackInfo(trackInfoVo, userId);
        return Result.ok();
    }


    /**
     * 上传音频文件
     *
     * @param file
     * @return
     */
    @Operation(summary = "上传音频文件")
    @PostMapping("/uploadTrack")
    public Result uploadTrack(MultipartFile file) {
        // 调用service的方法
        Map<String, Object> map = trackInfoService.uploadTrack(file);
        //返回结果
        return Result.ok(map);

    }
}

